<?php

namespace Home\Controller;
use core\lib\Exception;
use Home\Org\Util\TTXApi;
use Home\Org\Util\RedisClient;
use Home\Org\Util\Sqlserver;
/**
 * 通天晓WMS相关数据接口 2016-07-22
 * @author dem
 *
 */
class PlatformController  extends HomeController{
    public function index()
    {


    }

    public function platformList()
    {
        $this->display();
    }
    public function getPlatformList()
    {
        import("@.ORG.Util.Sqlserver");
        $pageIndex = I('pageIndex') +1 ;
        $pageSize = I('pageSize')?:50;
        $where=" where 1=1 ";
        $CardCode=I('CardCode');
        $ShortName=I('ShortName');
        if($CardCode){

            $where.= " and t.ShortName like '%". trim($ShortName) . "%'";
        }
        if($CardCode){
            $where.= " and t.CardCode = '". trim($CardCode) . "'";
        }
        $sqlCount = "select count(*) as total  from   [AX_ChannelList] t ".$where;
        $sqlser = new Sqlserver();
        $res = $sqlser->query($sqlCount);
        $count=$res[0]['total'];
        $pageStart=($pageIndex-1)*$pageSize+1;
        $pageEnd=$pageStart+$pageSize-1;
        $sql="Select * from ( select ROW_NUMBER() over(order by CardCode asc)  as Id,* From (select t.*  from AX_ChannelList t ".$where." )as a )as a Where a.Id between $pageStart And  $pageEnd";
        $list= $sqlser->query($sql);
        echo json_encode(array('total'=>$count,'data'=>$list));exit;
    }
    public function editPlatform()
    {
        import("@.ORG.Util.Sqlserver");
        $CardCode=I('CardCode');
        $sqlser = new Sqlserver();
        $sql = "select *  from   [AX_ChannelList] t  where t.CardCode='".$CardCode."'";
        $res = $sqlser->query(iconv("utf-8","gb2312",$sql));
        $data=$res[0];
        $this->assign('data',$data);
        $this->display();
    }

    public function editingPlatform()
    {
        import("@.ORG.Util.Sqlserver");
        $result=array('status'=>200,'msg'=>"更新成功");
        $CardCode=I('CardCode');
        $CManager=I('CManager');
        //$CManager=mb_convert_encoding($CManager,'GBK',mb_detect_encoding($CManager,array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5')));
        //$CManager=mb_convert_encoding($CManager,'gbk','utf-8');
        $ChannelType=I('ChannelType');
        $sqlser = new Sqlserver();
        try{
            $sql = "update   [AX_ChannelList] set  CManager='".$CManager."',ChannelType='".$ChannelType."'  where CardCode='".$CardCode."'";
            $res = $sqlser->saveBysql(iconv("utf-8","gb2312",$sql));
        }catch(Exception $e)
        {
            $result=array('status'=>201,'msg'=>"更新失败：".$e->getMessage());
        }
        echo json_encode($result);exit;

    }


}